package com.it.oauth2.custom.handle;

import com.alibaba.fastjson.JSON;
import com.it.base.resposne.JsonData;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @program: auth2
 * @description: 自定义认证失败处理器(token 失效 或者token 未认证)
 * @author: Lefnmg
 * @create: 2018-11-24 14:31
 **/
public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {

        JsonData data = new JsonData().put("code", 401).put("msg",e.getMessage());
        httpServletResponse.setContentType("application/json;charset=UTF-8");

        httpServletResponse.getWriter().write(JSON.toJSONString(data));

    }
}
